import request from "./request";
import type {
  User,
  Category,
  Knowledge,
  Question,
  ListResponse,
  ListParams,
} from "~/types";

export const authApi = {
  login: (data: { username: string; password: string }) =>
    request.post<{ token: string; user: User }>("/auth/login", data),
  changePassword: (data: { oldPassword: string; newPassword: string }) =>
    request.post("/auth/change-password", data),
};

export const categoryApi = {
  list: (params: { parentId?: string; parentLevel?: string }) =>
    request.get("/categories", { params }),
};

export const knowledgeApi = {
  list: (params: ListParams) =>
    request.get<ListResponse<Knowledge>>("/knowledge-points", { params }),
  create: (
    data: Omit<Knowledge, "id" | "createdAt" | "updatedAt" | "categoryName">
  ) => request.post<Knowledge>("/knowledge-points", data),
  update: (
    id: number,
    data: Partial<
      Omit<Knowledge, "id" | "createdAt" | "updatedAt" | "categoryName">
    >
  ) => request.put<Knowledge>(`/knowledge-points/${id}`, data),
  delete: (id: number) => request.delete(`/knowledge-points/${id}`),
};

export const questionApi = {
  list: (params: ListParams) =>
    request.get<ListResponse<Question>>("/question-bank", { params }),
  create: (
    data: Omit<Question, "id" | "createdAt" | "updatedAt" | "knowledgeName">
  ) => request.post<Question>("/question-bank", data),
  update: (
    id: number,
    data: Partial<
      Omit<Question, "id" | "createdAt" | "updatedAt" | "knowledgeName">
    >
  ) => request.patch<Question>(`/question-bank/${id}`, data),
  delete: (id: number) => request.delete(`/question-bank/${id}`),
};
